package servlet;

import bean.Result;
import biz.CartBiz;
import com.google.gson.Gson;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "InCartServlet", value = "/InCartServlet.s")
public class InCartServlet extends HttpServlet {
    private CartBiz biz = new CartBiz();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Result result;

        try {
            HttpSession session = request.getSession();
            String uid = String.valueOf(session.getAttribute("userid"));
            String product = request.getParameter("product");
            String number = request.getParameter("number");
            if (!biz.queryProduct(uid, product, number).equals(number)){
                biz.create(product, uid, number);
            }
            result = new Result(1, "添加成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(0, "添加失败", null);
        }
        String json = new Gson().toJson(result);
        response.getWriter().append(json);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
